<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Posts_Model extends CI_Model{
	protected $_name = "posts";
	protected $_primary = "PostId";
	var $Title_vn = "Title_vn", $Title_en = "Title_en", $Sumary_vn = "Sumary_vn", $Sumary_en = "Sumary_en", $Detail_vn = "Detail_vn", $Detail_en = "Detail_en", $CreateDate = "CreateDate", $Viewed = "Viewed", $Image = "Image", $TpId = "TpId", $UserId = "UserId", $Status = "Status";
	
	function numRowPostByTpId($TpId){
		$this->db->where($this->TpId, $TpId);
		return $this->db->get($this->_name)->num_rows();
	}
	
	function insertPost($urlImage){
		$this->db->set($this->Title_vn, trim($this->input->post('Title_vn', true)));
		$this->db->set($this->Title_en, trim($this->input->post('Title_en', true)));
		$this->db->set($this->Sumary_vn, trim($this->input->post('Sumary_vn', true)));
		$this->db->set($this->Sumary_en, trim($this->input->post('Sumary_en', true)));
		$this->db->set($this->Detail_vn, trim($this->input->post('Detail_vn')));
		$this->db->set($this->Detail_en, trim($this->input->post('Detail_en')));
		$this->db->set($this->CreateDate, date('Y-m-d H:i:s'));
		$this->db->set($this->Viewed, 0);
		$this->db->set($this->Image, trim($urlImage));
		$this->db->set($this->TpId, $this->input->post('typepost', true));
		$this->db->set($this->UserId, $this->session->userdata('UserId'));
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
	}
	
	function updatePost($PostId, $urlImage){
		$data = array(
					$this->Title_vn => trim($this->input->post('Title_vn', true)),
					$this->Title_en => trim($this->input->post('Title_en', true)),
					$this->Sumary_vn => trim($this->input->post('Sumary_vn', true)),
					$this->Sumary_en => trim($this->input->post('Sumary_en', true)),
					$this->Detail_vn => trim($this->input->post('Detail_vn')),
					$this->Detail_en => trim($this->input->post('Detail_en')),
					$this->Image => trim($urlImage),
					$this->TpId	=> $this->input->post('typepost', true),
				);
		$this->db->where($this->_primary, $PostId);
		$this->db->update($this->_name, $data);
	}
	
	function getPostLimitByTpId($TpId, $currentPage, $number){
		$this->db->select($this->_name.'.*, users.FullName as FullName');
		$this->db->where($this->TpId, $TpId);
		$this->db->join("users","users.UserId=".$this->_name.".".$this->UserId);
		$this->db->order_by($this->CreateDate, "DESC");
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	function countPostByTpId($TpId){
		$this->db->where($this->TpId, $TpId);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function getPostTop($TpId,$Limit){
		$this->db->where($this->TpId, $TpId);
		$this->db->order_by($this->Viewed, "DESC");
		$this->db->limit($Limit);
		$query = $this->db->get($this->_name);
		return $query->result();
	}
	
	function changeStatusPost($PostId, $Status){
		try{
			$this->db->where($this->_primary, $PostId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deletePost($PostId){
		try{
			$this->db->where($this->_primary, $PostId);
			$this->db->delete($this->_name);
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function getPostByPostId($PostId){
		$this->db->where($this->_primary, $PostId);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function searchPost($keysearch, $fieldsearch, $TpId){
		$this->db->select($this->_name.'.*, users.FullName as FullName');
		$this->db->join("users","users.UserId=".$this->_name.".".$this->UserId);
		$this->db->where($this->TpId, $TpId);
		if($fieldsearch == 1){
			$this->db->like($this->Title_vn, $keysearch);
		}else{
			$this->db->like("FullName", $keysearch);
		}
		$this->db->order_by($this->CreateDate, "DESC");
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
}